<?php
abstract class Core_model_abstract
{
	protected $_table;                 // 表名称（string）
	protected $_fields;                // 表字段(array)
	protected $_primaryKeys;           // 主键（array）
	protected $_autoIncrementFields;   // 自增键(array)
	protected $_foreignKeyRelation;    // 外键关系(array)
	
	public function __construct($tableName, $tablePre = '')
	{
		$tablePre = !empty($tablePre) ? $tablePre : Core_config::getConfig('DB_TABLE_PRE');
		// 设置主表名称
		$this->_table = $tablePre.$tableName;
		$this->_fields = $this->_setFields();
		$this->_primaryKeys = $this->_setPrimaryKeys();
		$this->_autoIncrementFields = $this->_setAutoIncrementFields();
		$this->_foreignKeyRelation = $this->_setForeignKeyRelation();
	}
	
	protected abstract function _setFields();
	protected abstract function _setPrimaryKeys();
	protected abstract function _setAutoIncrementFields();
	protected abstract function _setForeignKeyRelation();
	
	protected abstract function _validationField($fieldName, $value);
	
	/**
	 * 使传入的数据根据表字段处理符合字段的数据
	 * 
	 * @param array $data
	 * @return array
	 */
	protected function _getFieldsData($data)
	{
		$newData = array();
		foreach ($data as $key=>$value)
		{
			if(in_array($key, $this->_fields) && !in_array($key, $this->_autoIncrementFields))
			{
				if(!$this->_validationField($key, $value))
				{
					return false;
				}
				$newData[$key] = $value;
			}
		}
		
		return $newData;
	}
	
	protected function _getExistFields($fields)
	{
		$newFields = array();
		foreach ($fields as $field)
		{
			if(in_array($field, $this->_fields))
			{
				$newFields[] = $field;
			}
		}
		
		return $newFields;
	}
	
	protected function _select($params)
	{
		$fileds = isset($params['fileds']) ? $params['fileds'] : '*';
		$where = isset($params['where']) ? $params['where'] : '';
		$orderby = isset($params['orderby']) ? $params['orderby'] : '';
		$limit = isset($params['limit']) ? $params['limit'] : '';
		
		if(!empty($where))
		{
			$where = ' WHERE '.$where;
		}
		else
		{
			$where = '';
		}
		if(!empty($orderby))
		{
			$orderby = ' ORDER BY '.$orderby;
		}
		else
		{
			$orderby = '';
		}
		if(empty($fileds) || (is_string($fileds) && trim($fileds) == '*'))
		{
			$fileds = '*';
		}
		else if(is_string($fileds) || is_array($fileds))
		{
			if(is_string($fileds))
			{
				$fileds = explode(',', $fileds);
			}
			$filedsArray = $this->_getExistFields($fileds);
			if(empty($filedsArray))
			{
				
				$fileds = '*';
			}
			else 
			{
				$fileds = '`'.implode('`,`', $filedsArray).'`';
			}
		}
		if(!empty($limit))
		{
			$limit = ' LIMIT '.$limit;
		}
		else 
		{
			$limit = '';
		}
		
		$sql = "SELECT {$fileds} FROM `{$this->_table}`{$where}{$orderby}{$limit}";
		
		$statement = Core_db_slave::getSlave()->query($sql);
		return $statement->fetchAll(PDO::FETCH_ASSOC);
	}
	
	protected function _insert($data)
	{
		$fields = '';
		$values = '';
		$dot = '';
		
		foreach($data as $field=>$value)
		{
			$value = $this->chkAddslashes($value);
			$fields .= $dot.'`'.$field.'`';
			$values .= $dot."'".$value."'";
			$dot = ',';
		}
		
		$dbMaster = Core_db_master::getMaster();
		$sql = "INSERT INTO `{$this->_table}`({$fields}) VALUES({$values})";
		$sucess = $dbMaster->exec($sql);
		echo $sql;
		var_dump($sucess);
		var_dump($dbMaster->errorInfo());
		if(!$sucess)
		{
			var_dump($dbMaster->errorInfo());
		}
		return $sucess;
	}
	
	protected function chkAddslashes($str)
	{
		global $_G;
		
		if(!$_G['magic_quotes_gpc'])
		{
			return addslashes($str);
		}
		return $str;
	}
}